package cn.iocoder.yudao.module.acm.service.cleaningplan;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.acm.controller.admin.cleaningplan.vo.CleaningPlanPageReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.cleaningplan.vo.CleaningPlanRespVO;
import cn.iocoder.yudao.module.acm.controller.admin.cleaningplan.vo.CleaningPlanSaveReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.cleaningplan.vo.CleaningPlanUpdateStatusReqVO;
import cn.iocoder.yudao.module.acm.dal.dataobject.cleaningplan.CleaningPlanDO;
import cn.iocoder.yudao.module.acm.dal.dataobject.onboardsupply.OnboardSupplyDO;
import cn.iocoder.yudao.module.acm.dal.mysql.cleaningplan.CleaningPlanMapper;
import cn.iocoder.yudao.module.acm.dal.mysql.onboardsupply.OnboardSupplyMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.acm.enums.ErrorCodeConstants.CLEANING_PLAN_NOT_EXISTS;

/**
 * 机供品餐具清洗计划 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class CleaningPlanServiceImpl implements CleaningPlanService {

    @Resource
    private CleaningPlanMapper cleaningPlanMapper;
    @Resource
    private OnboardSupplyMapper onboardSupplyMapper;

    @Override
    public Long createCleaningPlan(CleaningPlanSaveReqVO createReqVO) {
        // 插入
        CleaningPlanDO cleaningPlan = BeanUtils.toBean(createReqVO, CleaningPlanDO.class);
        cleaningPlan.setStartDate(createReqVO.getExpireDate()[0]);
        cleaningPlan.setEndDate(createReqVO.getExpireDate()[1]);
        cleaningPlan.setCplanCode("C"+System.currentTimeMillis());
        cleaningPlanMapper.insert(cleaningPlan);
        // 返回
        return cleaningPlan.getId();
    }

    @Override
    public void updateCleaningPlan(CleaningPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateCleaningPlanExists(updateReqVO.getId());
        // 更新
        CleaningPlanDO updateObj = BeanUtils.toBean(updateReqVO, CleaningPlanDO.class);
        cleaningPlanMapper.updateById(updateObj);
    }

    @Override
    public void updateCleaningPlanStatus(CleaningPlanUpdateStatusReqVO updateReqVO) {
        // 校验存在
        validateCleaningPlanExists(updateReqVO.getId());
        // 更新
        CleaningPlanDO updateObj = BeanUtils.toBean(updateReqVO, CleaningPlanDO.class);
        cleaningPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteCleaningPlan(Long id) {
        // 校验存在
        validateCleaningPlanExists(id);
        // 删除
        cleaningPlanMapper.deleteById(id);
    }

    @Override
        public void deleteCleaningPlanListByIds(List<Long> ids) {
        // 校验存在
        validateCleaningPlanExists(ids);
        // 删除
        cleaningPlanMapper.deleteByIds(ids);
        }

    private void validateCleaningPlanExists(List<Long> ids) {
        List<CleaningPlanDO> list = cleaningPlanMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CLEANING_PLAN_NOT_EXISTS);
        }
    }

    private void validateCleaningPlanExists(Long id) {
        if (cleaningPlanMapper.selectById(id) == null) {
            throw exception(CLEANING_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public CleaningPlanDO getCleaningPlan(Long id) {
        return cleaningPlanMapper.selectById(id);
    }

    @Override
    public PageResult<CleaningPlanRespVO> getCleaningPlanPage(CleaningPlanPageReqVO pageReqVO) {
        if (ObjectUtil.isNotEmpty(pageReqVO.getExpireDate())) {
            pageReqVO.setStartDate(pageReqVO.getExpireDate()[0]);
            pageReqVO.setEndDate(pageReqVO.getExpireDate()[1]);
        }
        PageResult<CleaningPlanDO> result = cleaningPlanMapper.selectPage(pageReqVO);
        return new PageResult<>(buildTablewareDistPlanDetailList(result.getList()), result.getTotal());
    }

    private List<CleaningPlanRespVO> buildTablewareDistPlanDetailList(List<CleaningPlanDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        // 1 获取餐具列表
        List<OnboardSupplyDO> supplyDOS = onboardSupplyMapper.selectByIds(convertList(list, CleaningPlanDO::getTablewareType));
        Map<Long, OnboardSupplyDO> convertMap = CollectionUtils.convertMap(supplyDOS, OnboardSupplyDO::getId);
        // 2. 转换成 VO
        return BeanUtils.toBean(list, CleaningPlanRespVO.class, respVO ->
                MapUtils.findAndThen(convertMap, respVO.getTablewareType(), supply -> respVO.setTablewareName(supply.getName())));
    }

}